# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

pkgbase=bash
pkgname=('bash' 'bash-devel')
_basever=4.4
_patchlevel=023 #prepare for some patches
pkgver=${_basever}.${_patchlevel}
pkgrel=1
pkgdesc="The GNU Bourne Again shell"
arch=('i686' 'x86_64')
license=('GPL')
url="https://www.gnu.org/software/bash/bash.html"
groups=('base')
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey
makedepends=('gettext-devel' 'libreadline-devel>=7.0' 'ncurses-devel')
source=(https://ftp.gnu.org/gnu/bash/bash-${_basever}.tar.gz{,.sig}
        0001-bash-4.4-cygwin.patch
        0002-bash-4.3-msysize.patch
        0005-bash-4.3-msys2-fix-lineendings.patch
        0006-bash-4.3-add-pwd-W-option.patch
        0007-fix-static-build.patch)
if [ $_patchlevel -gt 000 ]; then
  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    source=(${source[@]} https://ftp.gnu.org/gnu/bash/bash-${_basever}-patches/bash${_basever//./}-$(printf "%03d" $p){,.sig})
  done
fi
noextract=(${pkgbase}-${_basever}.tar.gz)
validpgpkeys=('7C0135FB088AAF6C66C650B9BB5869F064EA74AB') # Chet Ramey

prepare() {
  [[ -d ${pkgbase}-${_basever} ]] && rm -rf ${pkgbase}-${_basever}
  tar -xzvf ${srcdir}/${pkgbase}-${_basever}.tar.gz || true
  cd ${srcdir}/${pkgname}-${_basever}

  # Remove patch-created file.
  [[ -f cross-build/msys32.cache ]] && rm -rf cross-build/msys32.cache

  for (( p=1; p<=$((10#${_patchlevel})); p++ )); do
    msg "applying patch bash${_basever//./}-$(printf "%03d" $p)"
    patch -p0 -i $srcdir/bash${_basever//./}-$(printf "%03d" $p)
  done

  patch -p1 -i ${srcdir}/0001-bash-4.4-cygwin.patch
  patch -p1 -i ${srcdir}/0002-bash-4.3-msysize.patch
  patch -p1 -i ${srcdir}/0005-bash-4.3-msys2-fix-lineendings.patch
  patch -p1 -i ${srcdir}/0006-bash-4.3-add-pwd-W-option.patch
  patch -p1 -i ${srcdir}/0007-fix-static-build.patch

  autoconf
}

build() {
  cd ${srcdir}/${pkgname}-$_basever

  ./configure --build=${CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --localstatedir=/var \
    --enable-static-link \
    --enable-readline \
    --without-libintl-prefix \
    --without-libiconv-prefix \
    --with-installed-readline \
    --without-bash-malloc \
    --with-curses \
    bash_cv_dev_stdin=present \
    bash_cv_dev_fd=standard \
    bash_cv_termcap_lib=libncurses

  # Prepopulate the release level.
  expr ${pkgrel} > .build || :

  # CFLAGS+=" -D_STATIC_BUILD"
  make CPPFLAGS=-DWORDEXP_OPTION \
       HISTORY_LDFLAGS= \
       READLINE_LDFLAGS= \
       LOCAL_LDFLAGS='-Wl,--export-all,--out-implib,lib$(@:.exe=.dll.a)'
}

check() {
  cd ${srcdir}/${pkgname}-$_basever
  make check
}

package_bash() {
  depends=('msys2-runtime') # 'libintl' 'libiconv' 'libreadline>=7.0' 'ncurses' 'gcc-libs'
  provides=('sh')
  #install=bash.install

  cd ${srcdir}/${pkgname}-$_basever
  make DESTDIR=${pkgdir} install

  cp -fp ${pkgdir}/usr/bin/{ba,}sh.exe

  cd ${pkgdir}/usr/share/man/man1 > /dev/null
  echo '.so man1/bash.1' > sh.1
  echo '.so man1/bash_builtins.1.gz' > alias.1

  gzip alias.1
  for f in bg bind break builtin caller case cd command compgen complete \
    continue declare dirs disown do done elif else enable esac eval exec \
    exit export fc fg fi for function getopts hash help history if in jobs \
    let local logout popd pushd read readonly return select set shift shopt \
    source suspend then time times trap type typeset ulimit umask unalias \
    unset until wait while [ ; do
    cp -fp alias.1.gz $f.1.gz
  done
}

package_bash-devel() {
 pkgdesc="Bash headers and libraries"
 groups=('development')
 options=('staticlibs')
 backup=()

 mkdir -p ${pkgdir}/usr/{include,lib}
 cd ${srcdir}/bash-$_basever
 cp -f libbash.dll.a ${pkgdir}/usr/lib/

 mkdir -p  ${pkgdir}/usr/include/bash
 for f in [^y]*.h builtins/*.h include/*.h lib/{glob,tilde}/*.h
 do
   /usr/bin/install ${f} ${pkgdir}/usr/include/bash
 done
}

sha256sums=('d86b3392c1202e8ff5a423b302e6284db7f8f435ea9f39b5b1b20fd3ac36dfcb'
            'SKIP'
            '58345cdc35ef7588df46b029c7785969a80b1b7c4853ec6703734f719d1a0969'
            'da08e4661fdcb179f7749dd1c8ce728d0ed8d4ca07e50da757046c06c6464dee'
            'c866eeb043090c13ab2ce6777ca6a24b31929fb58c4a6a221bca541fcc5d9949'
            '9ef0a4686385a36c7a094ea8f5e9e973ee427c868952b2778f2f659347f74b32'
            '82578a38798bfcfd85707c08e4f7b22c3fa7acc7a01d083faf86eb923511d60e'
            '3e28d91531752df9a8cb167ad07cc542abaf944de9353fe8c6a535c9f1f17f0f'
            'SKIP'
            '7020a0183e17a7233e665b979c78c184ea369cfaf3e8b4b11f5547ecb7c13c53'
            'SKIP'
            '51df5a9192fdefe0ddca4bdf290932f74be03ffd0503a3d112e4199905e718b2'
            'SKIP'
            'ad080a30a4ac6c1273373617f29628cc320a35c8cd06913894794293dc52c8b3'
            'SKIP'
            '221e4b725b770ad0bb6924df3f8d04f89eeca4558f6e4c777dfa93e967090529'
            'SKIP'
            '6a8e2e2a6180d0f1ce39dcd651622fb6d2fd05db7c459f64ae42d667f1e344b8'
            'SKIP'
            'de1ccc07b7bfc9e25243ad854f3bbb5d3ebf9155b0477df16aaf00a7b0d5edaf'
            'SKIP'
            '86144700465933636d7b945e89b77df95d3620034725be161ca0ca5a42e239ba'
            'SKIP'
            '0b6bdd1a18a0d20e330cc3bc71e048864e4a13652e29dc0ebf3918bea729343c'
            'SKIP'
            '8465c6f2c56afe559402265b39d9e94368954930f9aa7f3dfa6d36dd66868e06'
            'SKIP'
            'dd56426ef7d7295e1107c0b3d06c192eb9298f4023c202ca2ba6266c613d170d'
            'SKIP'
            'fac271d2bf6372c9903e3b353cb9eda044d7fe36b5aab52f21f3f21cd6a2063e'
            'SKIP'
            '1b25efacbc1c4683b886d065b7a089a3601964555bcbf11f3a58989d38e853b6'
            'SKIP'
            'a7f75cedb43c5845ab1c60afade22dcb5e5dc12dd98c0f5a3abcfb9f309bb17c'
            'SKIP'
            'd37602ecbeb62d5a22c8167ea1e621fcdbaaa79925890a973a45c810dd01c326'
            'SKIP'
            '501f91cc89fadced16c73aa8858796651473602c722bb29f86a8ba588d0ff1b1'
            'SKIP'
            '773f90b98768d4662a22470ea8eec5fdd8e3439f370f94638872aaf884bcd270'
            'SKIP'
            '5bc494b42f719a8b0d844b7bd9ad50ebaae560e97f67c833c9e7e9d53981a8cc'
            'SKIP'
            '27170d6edfe8819835407fdc08b401d2e161b1400fe9d0c5317a51104c89c11e'
            'SKIP'
            '1840e2cbf26ba822913662f74037594ed562361485390c52813b38156c99522c'
            'SKIP'
            'bd8f59054a763ec1c64179ad5cb607f558708a317c2bdb22b814e3da456374c1'
            'SKIP'
            '45331f0936e36ab91bfe44b936e33ed8a1b1848fa896e8a1d0f2ef74f297cb79'
            'SKIP'
            '4fec236f3fbd3d0c47b893fdfa9122142a474f6ef66c20ffb6c0f4864dd591b6'
            'SKIP')
